package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import data.GameState;
import data.IGameDataReadOnly;
import eolympics.Game;
import gui.MainFrame.Screens;

class HomeScreen
        extends ScreenParent
{
    private static final long                serialVersionUID = 3985021977712068091L;
    private final IMainFrameScreenController mIMainFrameScreenController;
    private String                           mPlayerName      = "noob";

    private final Game                       mGame;

    public HomeScreen(IMainFrameScreenController pIMainFrameScreenController, final Game pGame)
    {
        super();

        mGame = pGame;
        mIMainFrameScreenController = pIMainFrameScreenController;

        setLayout(new BorderLayout());

        JButton startAsServerButton = new JButton("Start as Server");
        add(startAsServerButton, BorderLayout.WEST);

        ActionListener startAsServerListener = new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                startGameAsServer();
            }
        };
        startAsServerButton.addActionListener(startAsServerListener);

        JButton startAsClientButton = new JButton("Start as Client");
        add(startAsClientButton, BorderLayout.EAST);

        ActionListener startAsClientListener = new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                startGameAsClientPreparation();
            }
        };
        startAsClientButton.addActionListener(startAsClientListener);
    }

    private void startGameAsServer()
    {
        try
        {
            mGame.startAsServer(mPlayerName);
            final int WAIT_TIME_FOR_SERVER_TO_START = 400;
            Thread.sleep(WAIT_TIME_FOR_SERVER_TO_START);
        }
        catch( Exception e )
        {
            System.out.println("Starting game as server failed: ");
            e.printStackTrace();

            JOptionPane.showMessageDialog(this, "Could not start game: " + e.getMessage(), "Failure!", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void startGameAsClientPreparation()
    {
        String ipAddress = JOptionPane.showInputDialog(this, "Please enter the server-IP.");

        if( ipAddress == null )
        {
            return;
        }

        startGameAsClient(ipAddress);
    }

    private void startGameAsClient( final String pIpAddress )
    {
        try
        {
            mGame.joinGameAt(pIpAddress, mPlayerName);
        }
        catch( IOException e )
        {
            e.printStackTrace();

            JOptionPane.showMessageDialog(this, "Could not connect to Server.", "Connecting Failure!", JOptionPane.ERROR_MESSAGE);

            mGame.stopGame();
        }
    }

    @Override
    public void gameStateChanged( final IGameDataReadOnly pGameData )
    {
        if( pGameData.getGameState() == GameState.WAITING_FOR_START )
        {
            mIMainFrameScreenController.changeScreen(Screens.START_GAME_SCREEN);
        }
    }

}
